package com.sxhuayuan.parking.aspects;

import javax.annotation.Resource;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.sxhuayuan.parking.compenent.RabbitMQConfig;
import com.sxhuayuan.parking.config.PartnerpayScene;
import com.sxhuayuan.parking.config.PayMode;
import com.sxhuayuan.parking.entity.Order;
import com.sxhuayuan.parking.queue.QueueService;
import com.sxhuayuan.parking.queue.msg.PartnerpayQueueMsg;
import com.sxhuayuan.parking.queue.msg.QueueMsg;
import com.sxhuayuan.parking.service.MemberService;
import com.sxhuayuan.parking.service.OrderService;
import com.sxhuayuan.parking.service.ParkingLotService;
import com.sxhuayuan.parking.service.PartnerpayService;
import com.sxhuayuan.parking.service.PluginService;
import com.sxhuayuan.parking.service.RedisService;

/**
 * 下单切面，如果是无感，则添加无感代扣队列
 * 
 * @author DELL
 *
 */
@Component("parkingOrderAspect")
@Aspect
public class ParkingOrderAspect {

	Logger log = LoggerFactory.getLogger(getClass());

	@Resource(name = "redisServiceImpl")
	RedisService redisService;
	@Resource(name = "partnerpayServiceImpl")
	PartnerpayService partnerpayService;
	@Resource(name = "parkingLotServiceImpl")
	ParkingLotService lotService;
	@Resource(name = "memberServiceImpl")
	MemberService memberService;
	@Resource(name = "orderServiceImpl")
	OrderService orderService;
	@Resource(name = "pluginServiceImpl")
	PluginService pluginService;
	@Resource(name = "rabbitMsgQueue")
	QueueService rabbitMsgQueue;

	@Pointcut(value = "execution(* com.sxhuayuan.parking.service.OrderService.create(..)) && args(com.sxhuayuan.parking.entity.ParkingIn,..)")
	public void createPartnerpayOrderPointCut() {
	}

	@AfterReturning(value = "createPartnerpayOrderPointCut()", returning = "order")
	public void afterCreatePartnerOrder(JoinPoint jp, Order order) throws Throwable {
		if (order.getPayMode() == PayMode.partnerpay) {
			log.debug("无感支付 order={}, carPlate={}", order.getSn(), order.getCarPlate());

			QueueMsg msg = new QueueMsg();
			msg.setId(order.getUniqueId());
			msg.setRetryPlan(PartnerpayQueueMsg.RETRY_PLAN);
			msg.setRetryTimes(0);
			msg.setDelayTime(0);
			msg.setTopic(RabbitMQConfig.QUEUE_DELAY_PARTNERPAY);
			PartnerpayQueueMsg body = new PartnerpayQueueMsg();
			body.setScene(PartnerpayScene.PARKING);
			body.setOrderSn(order.getSn());
			body.setCarPlate(order.getCarPlate());
			body.setExitId(order.getExitId());
			// body.setOutTime(DateUtils.format(order.getEndtime(),
			// DateUtils.YYYYMMDDHHMMSS));
			// body.setInTime(DateUtils.format(order.getStarttime(),
			// DateUtils.YYYYMMDDHHMMSS));
			// body.setParkingTime(DateUtils.diff(order.getStarttime(), order.getEndtime(),
			// Calendar.MINUTE));
			body.setFee(order.getMoney().floatValue());
			msg.setBody(JSON.toJSONString(body));
			log.debug("添加代扣消息队列：{}", msg.toString());
			try {
				rabbitMsgQueue.push(msg);
			} catch (Exception e) {
				log.debug("添加代扣消息队列失败: {}", msg.getId());
			}
		}
	}
}